<template>
  <div class="table-box">
    <el-form :model="searchForm" ref="searchFormRef" :inline="true" class="search-form">
      <el-form-item label="记录编号" prop="recordNo">
        <el-input v-model="searchForm.recordNo" placeholder="请输入记录编号" clearable />
      </el-form-item>
      <el-form-item label="设备名称" prop="equipmentName">
        <el-input v-model="searchForm.equipmentName" placeholder="请输入设备名称" clearable />
      </el-form-item>
      <el-form-item label="维护类型" prop="maintenanceType">
        <el-select v-model="searchForm.maintenanceType" placeholder="请选择类型" clearable style="width: 180px">
          <el-option label="日常点检" value="DAILY_CHECK" />
          <el-option label="定期保养" value="PERIODIC_MAINTENANCE" />
          <el-option label="故障维修" value="CORRECTIVE_MAINTENANCE" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="table-header">
      <el-button type="primary" :icon="CirclePlus" @click="handleAdd">新增维护记录</el-button>
    </div>

    <el-table :data="tableData" style="width: 100%" v-loading="loading" border>
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="recordNo" label="记录编号" />
      <el-table-column prop="equipmentName" label="设备名称" />
      <el-table-column prop="maintenanceType" label="维护类型">
        <template #default="scope">
          <el-tag :type="getTypeColor(scope.row.maintenanceType)">{{ getTypeText(scope.row.maintenanceType) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="maintainer" label="维护人员" />
      <el-table-column prop="maintenanceTime" label="维护时间" />
      <el-table-column prop="duration" label="耗时(分钟)" />
      <el-table-column prop="result" label="维护结果">
        <template #default="scope">
          <el-tag :type="getResultType(scope.row.result)">{{ getResultText(scope.row.result) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="scope">
          <el-button type="primary" link :icon="View" @click="handleView(scope.row)">查看</el-button>
          <el-button type="primary" link :icon="EditPen" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" link @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        :current-page="pagination.currentPage"
        :page-size="pagination.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="pagination.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
      />
    </div>
  </div>
</template>

<script setup lang="ts" name="maintenanceRecord">
import { ref, reactive, onMounted } from "vue";
import type { FormInstance } from "element-plus";
import { ElMessage, ElMessageBox } from "element-plus";
import { CirclePlus, EditPen, View } from "@element-plus/icons-vue";

const loading = ref(false);
const tableData = ref([]);
const searchFormRef = ref<FormInstance>();

const searchForm = reactive({
  recordNo: "",
  equipmentName: "",
  maintenanceType: ""
});

const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
});

const getTypeColor = (type: string) => {
  const typeMap: Record<string, string> = {
    DAILY_CHECK: "primary",
    PERIODIC_MAINTENANCE: "success",
    CORRECTIVE_MAINTENANCE: "danger"
  };
  return typeMap[type] || "info";
};

const getTypeText = (type: string) => {
  const typeMap: Record<string, string> = {
    DAILY_CHECK: "日常点检",
    PERIODIC_MAINTENANCE: "定期保养",
    CORRECTIVE_MAINTENANCE: "故障维修"
  };
  return typeMap[type] || type;
};

const getResultType = (result: string) => {
  const resultMap: Record<string, string> = {
    NORMAL: "success",
    ABNORMAL: "warning",
    FAILURE: "danger"
  };
  return resultMap[result] || "info";
};

const getResultText = (result: string) => {
  const resultMap: Record<string, string> = {
    NORMAL: "正常",
    ABNORMAL: "异常",
    FAILURE: "故障"
  };
  return resultMap[result] || result;
};

const getTableData = async () => {
  loading.value = true;
  try {
    console.log("获取维护记录数据");
  } catch (error) {
    console.error("获取维护记录列表失败:", error);
    ElMessage.error("获取维护记录列表失败");
  } finally {
    loading.value = false;
  }
};

const handleSearch = () => {
  pagination.currentPage = 1;
  getTableData();
};

const handleReset = () => {
  if (!searchFormRef.value) return;
  searchFormRef.value.resetFields();
  pagination.currentPage = 1;
  getTableData();
};

const handleSizeChange = (val: number) => {
  pagination.pageSize = val;
  getTableData();
};

const handleCurrentChange = (val: number) => {
  pagination.currentPage = val;
  getTableData();
};

const handleAdd = () => {
  ElMessage.info("新增维护记录功能待实现");
};

const handleView = (row: any) => {
  ElMessage.info("查看维护记录详情功能待实现");
};

const handleEdit = (row: any) => {
  ElMessage.info("编辑维护记录功能待实现");
};

const handleDelete = (row: any) => {
  ElMessageBox.confirm(`确认删除维护记录 "${row.recordNo}" 吗？`, "删除确认", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      ElMessage.success("删除成功");
      getTableData();
    })
    .catch(() => {});
};

onMounted(() => {
  getTableData();
});
</script>

<style scoped>
.search-form {
  background-color: #fff;
  padding: 20px;
  border-radius: 4px;
  margin-bottom: 16px;
}

.table-header {
  margin-bottom: 16px;
  display: flex;
  gap: 10px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
  flex-shrink: 0;
}
</style>